﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HRM.BLL;
using HRM.DAL.DAO;

namespace HRM.UI.HRAdminUI.EmpModule
{
    public partial class AddEmployeePhoto : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["empCode"] != null)
            {
                employeeCode.InnerText = Session["empCode"].ToString();
                textboxCode.Visible = false;
            }
            else
            {
                Label1.Visible = false;
                employeeCode.Visible = false;
                employeeCode.InnerText = "";
            }

        }

        public void PopulatePhoto(Photo photo)
        {
            try
            {

                PhotoManager manager = new PhotoManager();
                photo = manager.GetPhoto(photo);
                ShowEmpPhoto.ImageUrl = photo.FileName;



            }
            catch (Exception)
            {

                throw;
            }

        }
        protected void SavePhoto(object o, EventArgs e)
        {
            try
            {
                PhotoManager manager = new PhotoManager();
                Photo photo = new Photo();
                Code code = new Code();
                code.CodeName = txtEmployeeCode.Value;
                Employee employee = new Employee();
                employee.EmpCode = code;
                photo.Employee = employee;
                string photoName = Path.GetFileName(uploadimage.PostedFile.FileName);
                uploadimage.PostedFile.SaveAs(Server.MapPath("~/EmployeePhoto/") + code.CodeName + "-" + photoName);
                photo.FileName = "~/EmployeePhoto/" + code.CodeName + "-" + photoName;

                if (manager.SavePhoto(photo))
                {
                    lblImagemsg.ForeColor = Color.Green;
                    lblImagemsg.Text = "Photo saved suceessfully";
                    PopulatePhoto(photo);
                    ClearInputs();

                }
                else
                {
                    lblImagemsg.ForeColor = Color.Red;
                    lblImagemsg.Text = "Photo not Uploaded !";
                }

            }
            catch (Exception)
            {


                throw;
            }

        }

        protected void ClearInputs()
        {
            txtEmployeeCode.Value = "";
            //  uploadimage.Value = "";


        }
    }
}